<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html>
	<head>
		<title>增加渠道</title>
		<base href="<%=basePath%>" />
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="renderer" content="webkit">
		<meta http-equiv="Cache-Control" content="no-siteapp" />
		<meta name="apple-mobile-web-app-title" content="Amaze UI" />
		<link rel="stylesheet" href="layui/css/layui.css" media="all"> <!-- 适应所有设备 -->
		<style type="text/css">
		</style>
	</head>
	<% String nowDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); %>
	<body>
	    <!-- <form class="layui-form">
	      <div class="layui-form-item">
	        <label class="layui-form-label">渠道名称</label>
	        <div class="layui-input-block">
	          <input type="text" name="ChannelName" required="" lay-verify="required" placeholder="输入渠道名称" autocomplete="off" class="layui-input">
	        </div>
	      </div>
	      <div class="layui-form-item">
	        <label class="layui-form-label">版本控制</label>
	        <div class="layui-input-block">
	          <input type="text" name="VersionUpdate"  required="" lay-verify="required" placeholder="输入进入游戏最低版本" autocomplete="off" class="layui-input">
	        </div>
	      </div>
	      <div class="layui-form-item">
            <div class="layui-input-block">
              <button class="layui-btn" lay-submit lay-filter="changePassword">立即提交</button>
              <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
          </div>
	    </form> -->
	    <table class="layui-hide" id="Channel" lay-filter="Channel"></table>
	    
	    <script type="text/html" id="toolbar">
          <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addData">增加</button>
          </div>
        </script>
	    
	    <script type="text/html" id="modify">
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
	    
	    <script src="js/jquery.min.js"></script>
		<script src="layui/layui.js"></script>
		<script>
		    layui.use(['layer','form','table'],function(){
		    	var layer = layui.layer,
		    	    form  = layui.form,
		    	    table = layui.table;
		    	table.render({
		    		elem:'#Channel',
		    		url:'admin/getChannel',
		    		toolbar: '#toolbar', //开启头部工具栏，并为其绑定左侧模板
		    		defaultToolbar: [],  //隐藏右边工具栏
		    		method:'post',
		    		title:'渠道',
		    		height: 'full-200',
		    		skin: 'row',
		    		even: true,//开启隔行背景
		    		id:'AllTable',
		    		cellMinWidth : 100,
		    		cols:[[
		    			{field:'Name',title:'渠道名称 ',edit: 'text',style:'color:red'},
		    			{field:'Version',title:'版本号 ',edit: 'text',style:'color:blue'},
		    			{fixed:'right',title:'操作 ',toolbar: '#modify'},
		    			]],
		    			page : false,//分页
						response : {
							statusCode : 200
						//重新规定成功的状态码为 200，table 组件默认为 0
						},
						parseData : function(res) { //将原始数据解析成 table 组件所规定的数据
							return {
								"code" : res.status, //解析接口状态
								"msg" : res.message, //解析提示文本
								"count" : res.count, //解析数据长度
								"data" : res.data
							//解析数据列表
							};
						}
		    	});
		    	
		    	table.on('edit(Channel)',function(obj){
		    		var value = obj.value  //得到 修改后的值
		    		,data = obj.data  //得到所在行所有的键值
		    		,field = obj.field; //得到字段
		    		$.ajax({
		    			type:'POST',
		    			url:'admin/modifyChannel',
		    			async:true,
		    			data:{tableData:JSON.stringify(layui.table.cache.AllTable)},
		    			success:function(res){
		    				if(res == "" || res == null)
		    				layer.msg("修改成功",{icon:1});
    					    table.reload('AllTable', {});
		    			}
		    		});
		    	});
		    	
		    	//监听头工具栏事件
		    	table.on('toolbar(Channel)',function(obj){
		    		switch(obj.event){
		    		case 'addData':
		    			var tabledata = JSON.stringify(layui.table.cache.AllTable);
		    			var index = layer.open({
							type: 2,
							shadeClose: true,
							shade: 0.5,
							area: ['500px', '250px'],
							title: '增加渠道',
							skin: 'layer-ext-yourskin',
							content: '<%=basePath%>admin/AddChannel',
							success: function(layero,index,resp) {
								var body = layer.getChildFrame("body", index);
								body.find("#receiveData").val(tabledata);
								/* var data1 ={Version: "3", Name: "小石测试渠道3"};
				    			//push数组中 添加 数据
				    			layui.table.cache.AllTable.push(data1);
				    			var data2 = layui.table.cache.AllTable;
				    			table.reload('AllTable',{
				    				//当table采用url异步获取数据时，想要赋值 数据，重载表格，要清空url
				    				url:"",
				    				data:data2
				    			}); */
							},error:function(){
								
							},yes:function(index,layero){
								var obj = $(layero).find("iframe")[0].contentWindow;
								console.log(index);
							}
						})
		    			
		    		break;
		    		}
		    	});
		    	
		    	
		    	//监听行工具事件
		    	table.on('tool(Channel)',function(obj){
		    		var data = obj.data;
		    		if(obj.event === 'del'){
		    			layer.confirm('真的删除行么?',function(index){
		    				//删除table下标元素,为null
		    				delete layui.table.cache.AllTable[obj.tr.selector.split('"')[1]];
		    				//删除数组下标
		    				layui.table.cache.AllTable.splice(obj.tr.selector.split('"')[1],1);
		    				//获取下标值
		    				//console.log(obj.tr.selector.split('"')[1]);
		    				console.log(layui.table.cache.AllTable.length);
		    				layer.close(index);
			    			$.ajax({
				    			url:'admin/modifyChannel',
				    			async:true,
				    			type:'POST',
				    			data:{tableData:JSON.stringify(layui.table.cache.AllTable)},
				    			success:function(rsp){
				    				if(rsp == '' || rsp == null){
				    					setTimeout(function (){
				    						layer.msg("删除成功",{icon:1});
					    					layui.table.reload('AllTable',{});
										}, 500);
				    				}else{
				    					layer.msg(rsp,{icon:2})
				    				}
				    			},error:function(){
				    				layer.msg("删除失败",{icon:2})
				    			}
				    		});
		    			});
		    		}
		    	});
		    	
		    });
		</script>
    </body>
</html>